﻿using System.Web.Mvc;

namespace VedranKrizicPortfolio.Helpers
{
    public static class ArticleReducer
    {
        public static string ReduceArticle(this HtmlHelper helper, string input, int length)
        {
            if (input.Length <= length)
            {
                return input;
            }

            else
            {
                input = RemoveFrameTag(input);
                input = RemoveIMGTag(input);

                string finalString = input.Remove(length).Insert(length, "... ");
                return finalString;
            }
        }
        private static string RemoveFrameTag(string input)
        {
            if (input.Contains(@"<iframe"))
            {
                while (input.Contains(@"<iframe"))
                {
                    int begOn = input.IndexOf(@"<iframe");
                    int endOn = input.IndexOf(@"</iframe>", begOn);
                    input = input.Remove(begOn, endOn);
                }
            }
            return input;
        }
        private static string RemoveIMGTag(string input)
        {
            if (input.Contains(@"<img"))
            {
                while (input.Contains(@"<img"))
                {
                    int begOn = input.IndexOf(@"<img");
                    int endOn = input.IndexOf(@"/>", begOn);
                    input = input.Remove(begOn, endOn);
                }
            }
            return input;
        }
    }
}